package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import dao.User_InfoDao;
import dao.pojo.User_Info;

public class User_InfoDaoImpl extends BaseDaoImpl implements User_InfoDao {

	public void addUser_Info(User_Info userinfo) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="INSERT INTO user_info(id,stu_id,other_info1,other_info2,other_info3) VALUES (?,?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, userinfo.getId());
			pstmt.setString(2, userinfo.getStu_id());
			pstmt.setString(3, userinfo.getOther_info1());
			pstmt.setString(4, userinfo.getOther_info2());
			pstmt.setString(5, userinfo.getOther_info3());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
		
	}

	public void deleteUser_Info(User_Info userinfo) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="DELETE FROM user_info WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, userinfo.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}	
	}

	public User_Info getUser_Info(User_Info userinfo) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		User_Info user_info=null;
		ResultSet rs=null;
		String sql="SELECT * FROM user_info WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, userinfo.getId());
			rs=pstmt.executeQuery();
			while(rs.next()){
				user_info.setId(rs.getInt("id"));
				user_info.setStu_id(rs.getString("stu_id"));
				user_info.setOther_info1(rs.getString("other_info1"));
				user_info.setOther_info2(rs.getString("other_info2"));
				user_info.setOther_info3(rs.getString("other_info3"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
		return user_info;
	}

	public void saveUser_Info(User_Info userinfo) {
		Connection conn=getPool().getConnection();
		
		PreparedStatement pstmt=null;
		String sql="UPDATE user_info SET stu_id=?,other_info1=?,other_info2=?,other_info3=? WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, userinfo.getStu_id());
			pstmt.setString(2, userinfo.getOther_info1());
			pstmt.setString(3, userinfo.getOther_info2());
			pstmt.setString(4, userinfo.getOther_info3());
			pstmt.setInt(5, userinfo.getId());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			getPool().freeConnection(conn);
		}
		
	}

}
